package com.tgu.service.impl;

import com.tgu.service.IIconsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@Service
public class IconsServiceImpl implements IIconsService {

    @Value("${system-params.website}")
    private String website;

    @Override
    public byte[] getIconByName(String name) {

        byte[] bytes;

        // 获取路径
        String path = this.getClass().getResource("/static/icons/" + name).getPath();
        File file = new File(path);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return bytes;
    }

    @Override
    public String upLoadImg(HttpServletRequest req, MultipartFile file, Model m) {
        String path = null;
        //1. 接受上传的文件 @RequestParam("file") MultipartFile file
        try {
            //2.根据时间戳创建新的文件名，这样即便是第二次上传相同名称的文件，也不会把第一次的文件覆盖了
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            path = fileName; //保存文件名
            //3.通过req.getServletContext().getRealPath("") 获取当前项目的真实路径，然后拼接前面的文件名
//            String destFileName = req.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;

            // yyc版获取路径，获取当前项目路径的上一级路径，并设置文件存放文件夹
//            String property = System.getProperty("user.dir") + "\\..\\uploadImg" + File.separator + fileName; windows
            String property = System.getProperty("user.dir") + File.separator + "uploadImg" + File.separator + fileName;

//            String destFileName = path + File.separator + fileName;
            //4.第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录（创建到了webapp下uploaded文件夹下）
//            File destFile = new File(destFileName);
            File destFile = new File(property);
            destFile.mkdirs();
            //5.把浏览器上传的文件复制到希望的位置
            file.transferTo(destFile);
            //6.把文件名放在model里，以便后续显示用
            m.addAttribute("fileName", fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
//            return "上传失败," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
//            return "上传失败," + e.getMessage();
        }
        return website + "/portal/icons/getUpLoadedImg/" + path;

        // 重新获取上传文件的二进制文件
//        byte[] bytes;
//        File file1 = new File(path);
//        FileInputStream inputStream = null;
//        try {
//            inputStream = new FileInputStream(file1);
//            bytes = new byte[inputStream.available()];
//            inputStream.read(bytes, 0, inputStream.available());
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (null != inputStream) {
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//
//        return bytes;
    }

    @Override
    public byte[] getUpLoadedImg(String fileName) {
        // 拼接文件路径
//        String property = System.getProperty("user.dir") + "\\..\\uploadImg" + File.separator + fileName;
        String property = System.getProperty("user.dir") + File.separator + "uploadImg" + File.separator + fileName;

        File file = new File(property);
        FileInputStream inputStream = null;
        byte[] bytes;
        try {
            inputStream = new FileInputStream(file);
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return bytes;
    }
}
